﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class StatementBLL
    {
        private static readonly IAccessorialChargesDA accessorigalChargesDA = DataAccess.CreateAccessorialChargesDA();
        private static readonly IBillingCredenceDA billingCredenceDA = DataAccess.CreateBillingCredenceDA();
        private static readonly IBillingDailyInvDA billingDailyInvDA = DataAccess.CreateBillingDailyInvDA();
        private static readonly IBillingEntityDA billingEntityDA = DataAccess.CreateBillingEntityDA();
        private static readonly ICarrierDA carrierDA = DataAccess.CreateCarrierDA();
        private static readonly IChargesDetailDA chargesDetailDA = DataAccess.CreateChargesDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICustomerDA customerDA = DataAccess.CreateCustomerDA();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IStatementDA statementDA = DataAccess.CreateStatementDA();
        private static readonly IStatementDetailDA statementDetailDA = DataAccess.CreateStatementDetailDA();
        private static readonly ITradePartnerDA tradePartnerDA = DataAccess.CreateTradePartnerDA();
        private static readonly IVendorDA vendorDA = DataAccess.CreateVendorDA();

        public StatementBLL()
        {
            this.pubParasInfo = null;
        }

        public StatementBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public StatementBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteStatement(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<StatementInfo> statementInfoInfos = new List<StatementInfo>();
                StatementInfo statementInfoInfo = null;
                foreach (string id in IDS)
                {
                    statementInfoInfo = statementDA.GetStatementByID(dataBase, tran, id, this.dataBaseID[0]);
                    statementInfoInfos.Add(statementInfoInfo);
                    List<StatementDetailInfo> statementDetailInfos = statementDetailDA.GetStatementDetailByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    if ((statementDetailInfos != null) && (statementDetailInfos.Count > 0))
                    {
                        i = 0;
                        while (i < statementDetailInfos.Count)
                        {
                            BillingCredenceInfo billingCredenceInfo = new BillingCredenceInfo {
                                IsStatement = "N",
                                StatementID = "",
                                BillingCredenceID = statementDetailInfos[i].BillingCredenceID
                            };
                            billingCredenceDA.UpdateBillingCredenceByStatement(dataBase, tran, billingCredenceInfo, this.dataBaseID);
                            billingCredenceInfo = billingCredenceDA.GetBillingCredenceByID(dataBase, tran, statementDetailInfos[i].BillingCredenceID, this.dataBaseID[0], this.pubParasInfo);
                            if (((billingCredenceInfo != null) && ((billingCredenceInfo.CredenceType == "StorageCharges") || (billingCredenceInfo.CredenceType == "InvCharges"))) && !billingCredenceDA.CheckBillingCredenceStatementByTypeAndID(dataBase, tran, billingCredenceInfo.BillingObjectID, billingCredenceInfo.CredenceType, billingCredenceInfo.DocID, "Y", billingCredenceInfo.DocType, this.dataBaseID[0]))
                            {
                                BillingDailyInvInfo billingDailyInvInfo = new BillingDailyInvInfo {
                                    Days = billingCredenceInfo.DocID,
                                    OwnerID = billingCredenceInfo.BillingObjectID,
                                    IsStatement = "N",
                                    UpdatedBy = this.pubParasInfo.UserID
                                };
                                if (billingCredenceInfo.CredenceType == "StorageCharges")
                                {
                                    billingDailyInvInfo.IsStorageCharges = "Y";
                                }
                                else if (billingCredenceInfo.CredenceType == "InvCharges")
                                {
                                    billingDailyInvInfo.IsStorageCharges = "N";
                                }
                                billingDailyInvDA.UpdateBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                            }
                            i++;
                        }
                    }
                    statementDetailDA.DeleteStatementDetail(dataBase, tran, id, this.dataBaseID[0]);
                }
                if (statementDA.DeleteStatement(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < IDS.Count; i++)
                    {
                        new AuditDataBLL().InsertAuditData<StatementInfo>(dataBase, tran, statementInfoInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "STATEMENT", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp.Substring(0, temp.Length - 1);
        }

        public DataSet GetAllStatementInfo()
        {
            return statementDA.GetAllStatement(this.dataBaseID[0]);
        }

        public StatementInfo GetStatementInfoByID(string id)
        {
            StatementInfo statementInfo = statementDA.GetStatementByID(id, this.dataBaseID[0]);
            if (statementInfo != null)
            {
                statementInfo.StatementDetailInfos = statementDetailDA.GetStatementDetailByID(id, this.dataBaseID[0], this.pubParasInfo);
            }
            return statementInfo;
        }

        public DataSet GetStatementInfoByQueryEntity(StatementQueryEntity statementEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(statementEntity.StatementID))
            {
                sqlWhere.Add("S.STATEMENT_ID " + statementEntity.StatementID);
            }
            if (!string.IsNullOrEmpty(statementEntity.Status))
            {
                sqlWhere.Add(" S.STATUS " + statementEntity.Status);
            }
            if (!string.IsNullOrEmpty(statementEntity.BillingObject))
            {
                sqlWhere.Add(" S.BILLING_OBJECT " + statementEntity.BillingObject);
            }
            if (!string.IsNullOrEmpty(statementEntity.BillingObjectID))
            {
                sqlWhere.Add(" S.BILLING_OBJECT_ID " + statementEntity.BillingObjectID);
            }
            if (!string.IsNullOrEmpty(statementEntity.DueFromTo))
            {
                sqlWhere.Add(" S.DUE_FROM_TO " + statementEntity.DueFromTo);
            }
            return statementDA.GetStatementByQueryList(sqlWhere, statementEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public string InsertStatement(StatementInfo obj)
        {
            int i;
            string whLoginID = this.dataBaseID[0];
            string Statement = string.Empty;
            bool billingObject = true;
            string billingObjectName = string.Empty;
            int lineID = 10;
            decimal credenceTotalAmount = 0M;
            decimal statementTotalAmount = 0M;
            List<StatementDetailInfo> statementDetailListInfo = new List<StatementDetailInfo>();
            for (i = 0; i < obj.StatementDetailInfos.Count; i++)
            {
                BillingCredenceInfo selectBillingCredenceInfo = new BillingCredenceInfo {
                    CredenceType = obj.StatementDetailInfos[i].CredenceType,
                    BillingObject = obj.StatementDetailInfos[i].BillingObject,
                    BillingObjectID = obj.StatementDetailInfos[i].BillingObjectID,
                    DueFromTo = obj.StatementDetailInfos[i].DueFromTo,
                    CostType = obj.StatementDetailInfos[i].CostType,
                    DocID = obj.StatementDetailInfos[i].OrderID,
                    IsStatement = "N",
                    StatementID = string.Empty
                };
                List<BillingCredenceInfo> billingCredenceListInfo = billingCredenceDA.GetBillingCredenceListByCondition(selectBillingCredenceInfo, whLoginID, this.pubParasInfo);
                if ((billingCredenceListInfo != null) && (billingCredenceListInfo.Count > 0))
                {
                    for (int j = 0; j < billingCredenceListInfo.Count; j++)
                    {
                        StatementDetailInfo statementDetailInfo;
                        statementDetailInfo = new StatementDetailInfo();
                        statementDetailInfo.BillingCredenceID = billingCredenceListInfo[j].BillingCredenceID;
                        statementDetailInfo.OrderID = billingCredenceListInfo[j].DocID;
                        statementDetailInfo.OrderLineID = billingCredenceListInfo[j].DocLineID;
                        statementDetailInfo.LineID = lineID;
                        statementDetailInfo.CreatedBy = obj.CreatedBy;
                        statementDetailInfo.CreatedDate = DateTime.Now;
                        statementDetailInfo.UpdatedBy = obj.CreatedBy;
                        statementDetailInfo.UpdatedDate = statementDetailInfo.CreatedDate;
                        lineID += 10;
                        credenceTotalAmount += billingCredenceListInfo[j].BillingAmount;
                        statementDetailListInfo.Add(statementDetailInfo);
                    }
                }
                statementTotalAmount += obj.StatementDetailInfos[i].Receivable;
            }
            if (credenceTotalAmount != statementTotalAmount)
            {
                PublicMethod.GetInstance().ShowAlertMessage("F9200_006", this.pubParasInfo.UserLanguage);
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (statementDA.CheckStatementIDUnique(dataBase, tran, obj.StatementID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9200_002", this.pubParasInfo.UserLanguage, new List<string> { obj.StatementID });
                }
                if (string.IsNullOrEmpty(obj.StatementID))
                {
                    obj.StatementID = commonMethod.GenNextNumStr(dataBase, tran, "STATEMENT", whLoginID);
                }
                if (obj.BillingObject.ToUpper() == "Owner".ToUpper())
                {
                    TradePartnerInfo tradePartnerInfo = new TradePartnerInfo();
                    tradePartnerInfo = tradePartnerDA.GetTradePartnerByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (tradePartnerInfo != null)
                    {
                        billingObjectName = tradePartnerInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                else if (obj.BillingObject.ToUpper() == "Vendor".ToUpper())
                {
                    VendorInfo vendorInfo = new VendorInfo();
                    vendorInfo = vendorDA.GetVendorByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (vendorInfo != null)
                    {
                        billingObjectName = vendorInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                else if (obj.BillingObject.ToUpper() == "Customer".ToUpper())
                {
                    CustomerInfo customerInfo = new CustomerInfo();
                    customerInfo = customerDA.GetCustomerByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (customerInfo != null)
                    {
                        billingObjectName = customerInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                else if (obj.BillingObject.ToUpper() == "Carrier".ToUpper())
                {
                    CarrierInfo carrierInfo = new CarrierInfo();
                    carrierInfo = carrierDA.GetCarrierByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (carrierInfo != null)
                    {
                        billingObjectName = carrierInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                else if (obj.BillingObject.ToUpper() == "BillingEntity".ToUpper())
                {
                    BillingEntityInfo billingEntityInfo = new BillingEntityInfo();
                    billingEntityInfo = billingEntityDA.GetBillingEntityByID(dataBase, tran, obj.BillingObjectID, whLoginID, this.pubParasInfo);
                    if (billingEntityInfo != null)
                    {
                        billingObjectName = billingEntityInfo.Company;
                    }
                    else
                    {
                        billingObject = false;
                    }
                }
                if (!billingObject)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9180_003", this.pubParasInfo.UserLanguage, new List<string> { obj.BillingObjectID });
                }
                obj.UpdatedBy = this.pubParasInfo.UserID;
                if (statementDA.InsertStatement(dataBase, tran, obj, whLoginID) > 0)
                {
                    for (i = 0; i < statementDetailListInfo.Count; i++)
                    {
                        string billingCredenceID = statementDetailListInfo[i].BillingCredenceID;
                        statementDetailListInfo[i].StatementID = obj.StatementID;
                        statementDetailDA.InsertStatementDetail(dataBase, tran, statementDetailListInfo[i], whLoginID);
                        BillingCredenceInfo billingCredenceInfo = new BillingCredenceInfo {
                            IsStatement = "Y",
                            StatementID = obj.StatementID,
                            BillingCredenceID = billingCredenceID
                        };
                        billingCredenceDA.UpdateBillingCredenceByStatement(dataBase, tran, billingCredenceInfo, this.dataBaseID);
                        billingCredenceInfo = billingCredenceDA.GetBillingCredenceByID(dataBase, tran, billingCredenceID, whLoginID, this.pubParasInfo);
                        if (billingCredenceInfo != null)
                        {
                            BillingDailyInvInfo billingDailyInvInfo;
                            if (billingCredenceInfo.CredenceType == "StorageCharges")
                            {
                                billingDailyInvInfo = billingDailyInvDA.GetBillingDailyInvByID(dataBase, tran, billingCredenceInfo.DocID, billingCredenceInfo.BillingObjectID, "Y", whLoginID);
                                if ((billingDailyInvInfo != null) && (billingDailyInvInfo.IsStatement != "Y"))
                                {
                                    billingDailyInvInfo.OwnerID = billingCredenceInfo.BillingObjectID;
                                    billingDailyInvInfo.Days = billingCredenceInfo.DocID;
                                    billingDailyInvInfo.IsStatement = "Y";
                                    billingDailyInvInfo.IsStorageCharges = "Y";
                                    billingDailyInvInfo.UpdatedBy = this.pubParasInfo.UserID;
                                    billingDailyInvDA.UpdateBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                                }
                            }
                            else if (billingCredenceInfo.CredenceType == "InvCharges")
                            {
                                billingDailyInvInfo = billingDailyInvDA.GetBillingDailyInvByID(dataBase, tran, billingCredenceInfo.DocID, billingCredenceInfo.BillingObjectID, "N", whLoginID);
                                if ((billingDailyInvInfo != null) && (billingDailyInvInfo.IsStatement != "Y"))
                                {
                                    billingDailyInvInfo.OwnerID = billingCredenceInfo.BillingObjectID;
                                    billingDailyInvInfo.Days = billingCredenceInfo.DocID;
                                    billingDailyInvInfo.IsStatement = "Y";
                                    billingDailyInvInfo.IsStorageCharges = "N";
                                    billingDailyInvInfo.UpdatedBy = this.pubParasInfo.UserID;
                                    billingDailyInvDA.UpdateBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                                }
                            }
                        }
                    }
                    new AuditDataBLL().InsertAuditData<StatementInfo>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "STATEMENT", whLoginID);
                }
                Statement = obj.StatementID;
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return Statement;
        }

        public void SelectAccessorialChargesByStatement(DataBase database, DbTransaction tran, List<StatementDetailInfo> statementDetailInfo, string whLoginID)
        {
            List<StatementDetailInfo> statmentDetailInfoList = new List<StatementDetailInfo>();
            ChargesDetailBLL chargesDetailBLL = new ChargesDetailBLL();
            int iLineID = statementDetailInfo.Count + 1;
            foreach (StatementDetailInfo info in statementDetailInfo)
            {
                if (info.Type != RateChargeType.Inventory)
                {
                    List<AccessorialChargesInfo> accessorialChargesInfoList = accessorigalChargesDA.GetAccessorialChargesByOrderID(database, tran, info.OrderID, whLoginID);
                    if ((accessorialChargesInfoList != null) && (accessorialChargesInfoList.Count > 0))
                    {
                        for (int i = 0; accessorialChargesInfoList.Count > i; i++)
                        {
                            StatementDetailInfo statmentDetailInfo = new StatementDetailInfo {
                                LineID = iLineID * 10,
                                OrderID = accessorialChargesInfoList[i].OrderID,
                                OrderLineID = accessorialChargesInfoList[i].OrderLineID,
                                Receivable = decimal.Parse(accessorialChargesInfoList[i].RateID),
                                Type = accessorialChargesInfoList[i].AccessorialType,
                                SourceType = RateChargeType.Accessorial,
                                CreatedBy = this.pubParasInfo.UserID,
                                UpdatedBy = this.pubParasInfo.UserID,
                                WhID = accessorialChargesInfoList[i].WhID,
                                SkuID = accessorialChargesInfoList[i].SkuID,
                                Qty = accessorialChargesInfoList[i].Qty,
                                StatementID = info.StatementID
                            };
                            statmentDetailInfoList.Add(statmentDetailInfo);
                            accessorigalChargesDA.UpdateAccessorialChargesIsChecked(database, tran, accessorialChargesInfoList[i].AccessorialChargesID, whLoginID);
                            iLineID++;
                        }
                    }
                }
            }
            if (statmentDetailInfoList.Count > 0)
            {
                foreach (StatementDetailInfo infos in statmentDetailInfoList)
                {
                    statementDetailDA.InsertStatementDetail(database, tran, infos, whLoginID);
                }
            }
        }

        public void SelectChargesDetailByStatement(DataBase database, DbTransaction tran, StatementInfo statementInfo, string whLoginID)
        {
            if (statementInfo != null)
            {
                statementDetailDA.DeleteStatementDetail(database, tran, statementInfo.StatementID, whLoginID);
                List<string> Sqlwhere = new List<string> {
                    " OWNER_ID = '" + statementInfo.OwnerID + "'",
                    " EXECUTE_DATE <= '" + statementInfo.EndTime + "'"
                };
                if (((statementInfo.InBound == "Y") || (statementInfo.OutBound == "Y")) || (statementInfo.Inventory == "Y"))
                {
                    string strTemp = string.Empty;
                    if (statementInfo.InBound == "Y")
                    {
                        strTemp = strTemp + "'" + RateChargeType.InBound + "',";
                    }
                    if (statementInfo.OutBound == "Y")
                    {
                        strTemp = strTemp + "'" + RateChargeType.OutBound + "',";
                    }
                    if (statementInfo.Inventory == "Y")
                    {
                        strTemp = strTemp + "'" + RateChargeType.Inventory + "',";
                    }
                    if (!string.IsNullOrEmpty(strTemp))
                    {
                        Sqlwhere.Add(" TYPE IN (" + strTemp.Substring(0, strTemp.Length - 1) + ")");
                    }
                }
                List<ChargesDetailInfo> chargesDetailList = chargesDetailDA.GetChargesDetailByID(database, tran, Sqlwhere, this.pubParasInfo.UserID, whLoginID);
                if ((chargesDetailList != null) && (chargesDetailList.Count > 0))
                {
                    List<StatementDetailInfo> statmentDetailInfoList = new List<StatementDetailInfo>();
                    for (int i = 0; chargesDetailList.Count > i; i++)
                    {
                        StatementDetailInfo statmentDetailInfo = new StatementDetailInfo {
                            LineID = (i + 1) * 10
                        };
                        if (!string.IsNullOrEmpty(chargesDetailList[i].OrderID))
                        {
                            statmentDetailInfo.OrderID = chargesDetailList[i].OrderID;
                            statmentDetailInfo.OrderLineID = chargesDetailList[i].OrderLineID;
                        }
                        else
                        {
                            statmentDetailInfo.OrderID = chargesDetailList[i].ExecuteDate.ToString("yyyy-MM-dd");
                        }
                        statmentDetailInfo.Receivable = chargesDetailList[i].ChargeAmount;
                        statmentDetailInfo.Type = chargesDetailList[i].Type;
                        statmentDetailInfo.SourceType = chargesDetailList[i].SourceType;
                        statmentDetailInfo.StatementID = statementInfo.StatementID;
                        statmentDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                        statmentDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                        statmentDetailInfo.WhID = chargesDetailList[i].WhID;
                        statmentDetailInfo.SkuID = chargesDetailList[i].SkuID;
                        statmentDetailInfo.Qty = chargesDetailList[i].Qty;
                        statmentDetailInfoList.Add(statmentDetailInfo);
                        chargesDetailDA.UpdateChargesDetailIsChecked(database, tran, chargesDetailList[i].RowID, whLoginID);
                    }
                    if (statmentDetailInfoList.Count > 0)
                    {
                        foreach (StatementDetailInfo info in statmentDetailInfoList)
                        {
                            statementDetailDA.InsertStatementDetail(database, tran, info, whLoginID);
                        }
                        if (statementInfo.Accessorial == "Y")
                        {
                            this.SelectAccessorialChargesByStatement(database, tran, statmentDetailInfoList, whLoginID);
                        }
                    }
                }
            }
        }

        public string UpdateStatement(StatementInfo obj)
        {
            int i;
            string whLoginID = this.dataBaseID[0];
            int lineID = 10;
            decimal credenceTotalAmount = 0M;
            decimal statementTotalAmount = 0M;
            List<StatementDetailInfo> statementDetailListInfo = new List<StatementDetailInfo>();
            for (i = 0; i < obj.StatementDetailInfos.Count; i++)
            {
                BillingCredenceInfo selectBillingCredenceInfo = new BillingCredenceInfo {
                    CredenceType = obj.StatementDetailInfos[i].CredenceType,
                    BillingObject = obj.StatementDetailInfos[i].BillingObject,
                    BillingObjectID = obj.StatementDetailInfos[i].BillingObjectID,
                    DueFromTo = obj.StatementDetailInfos[i].DueFromTo,
                    CostType = obj.StatementDetailInfos[i].CostType,
                    DocID = obj.StatementDetailInfos[i].OrderID,
                    IsStatement = "Y",
                    StatementID = obj.StatementID
                };
                List<BillingCredenceInfo> billingCredenceListInfo = billingCredenceDA.GetBillingCredenceListByCondition(selectBillingCredenceInfo, whLoginID, this.pubParasInfo);
                if ((billingCredenceListInfo != null) && (billingCredenceListInfo.Count > 0))
                {
                    for (int j = 0; j < billingCredenceListInfo.Count; j++)
                    {
                        StatementDetailInfo statementDetailInfo;
                        statementDetailInfo = new StatementDetailInfo();
                        statementDetailInfo.BillingCredenceID = billingCredenceListInfo[j].BillingCredenceID;
                        statementDetailInfo.OrderID = billingCredenceListInfo[j].DocID;
                        statementDetailInfo.OrderLineID = billingCredenceListInfo[j].DocLineID;
                        statementDetailInfo.LineID = lineID;
                        statementDetailInfo.CreatedBy = obj.CreatedBy;
                        statementDetailInfo.CreatedDate = DateTime.Now;
                        statementDetailInfo.UpdatedBy = obj.CreatedBy;
                        statementDetailInfo.UpdatedDate = statementDetailInfo.CreatedDate;
                        statementDetailInfo.StatementID = obj.StatementID;
                        lineID += 10;
                        credenceTotalAmount += billingCredenceListInfo[j].BillingAmount;
                        statementDetailListInfo.Add(statementDetailInfo);
                    }
                }
                statementTotalAmount += obj.StatementDetailInfos[i].Receivable;
            }
            if (credenceTotalAmount != statementTotalAmount)
            {
                PublicMethod.GetInstance().ShowAlertMessage("F9200_006", this.pubParasInfo.UserLanguage);
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string StatementID = string.Empty;
            try
            {
                BillingCredenceInfo billingCredenceInfo;
                BillingDailyInvInfo billingDailyInvInfo;
                if (!statementDA.CheckStatementIDUnique(dataBase, tran, obj.StatementID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9200_001", this.pubParasInfo.UserLanguage, new List<string> { obj.StatementID });
                }
                List<StatementDetailInfo> statementDetailInfos = statementDetailDA.GetStatementDetailByID(dataBase, tran, obj.StatementID, whLoginID, this.pubParasInfo);
                if ((statementDetailInfos != null) && (statementDetailInfos.Count > 0))
                {
                    for (i = 0; i < statementDetailInfos.Count; i++)
                    {
                        billingCredenceInfo = new BillingCredenceInfo {
                            IsStatement = "N",
                            StatementID = "",
                            BillingCredenceID = statementDetailInfos[i].BillingCredenceID
                        };
                        billingCredenceDA.UpdateBillingCredenceByStatement(dataBase, tran, billingCredenceInfo, this.dataBaseID);
                        billingCredenceInfo = billingCredenceDA.GetBillingCredenceByID(dataBase, tran, statementDetailInfos[i].BillingCredenceID, whLoginID, this.pubParasInfo);
                        if (((billingCredenceInfo != null) && ((billingCredenceInfo.CredenceType == "StorageCharges") || (billingCredenceInfo.CredenceType == "InvCharges"))) && !billingCredenceDA.CheckBillingCredenceStatementByTypeAndID(dataBase, tran, billingCredenceInfo.BillingObjectID, billingCredenceInfo.CredenceType, billingCredenceInfo.DocID, "Y", billingCredenceInfo.DocType, whLoginID))
                        {
                            billingDailyInvInfo = new BillingDailyInvInfo {
                                OwnerID = billingCredenceInfo.BillingObjectID,
                                Days = billingCredenceInfo.DocID,
                                IsStatement = "N",
                                UpdatedBy = this.pubParasInfo.UserID
                            };
                            if (billingCredenceInfo.CredenceType == "StorageCharges")
                            {
                                billingDailyInvInfo.IsStorageCharges = "Y";
                            }
                            else if (billingCredenceInfo.CredenceType == "InvCharges")
                            {
                                billingDailyInvInfo.IsStorageCharges = "N";
                            }
                            billingDailyInvDA.UpdateBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                        }
                    }
                }
                statementDetailDA.DeleteStatementDetail(dataBase, tran, obj.StatementID, whLoginID);
                StatementInfo oldstatementInfoInfo = statementDA.GetStatementByID(dataBase, tran, obj.StatementID, this.dataBaseID[0]);
                obj.UpdatedBy = this.pubParasInfo.UserID;
                obj.UpdatedDate = DateTime.Now;
                if (statementDA.UpdateStatement(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < statementDetailListInfo.Count; i++)
                    {
                        string billingCredenceID = statementDetailListInfo[i].BillingCredenceID;
                        statementDetailDA.InsertStatementDetail(dataBase, tran, statementDetailListInfo[i], whLoginID);
                        billingCredenceInfo = new BillingCredenceInfo {
                            IsStatement = "Y",
                            StatementID = obj.StatementID,
                            BillingCredenceID = billingCredenceID
                        };
                        billingCredenceDA.UpdateBillingCredenceByStatement(dataBase, tran, billingCredenceInfo, this.dataBaseID);
                        billingCredenceInfo = billingCredenceDA.GetBillingCredenceByID(dataBase, tran, billingCredenceID, whLoginID, this.pubParasInfo);
                        if (billingCredenceInfo != null)
                        {
                            if (billingCredenceInfo.CredenceType == "StorageCharges")
                            {
                                billingDailyInvInfo = billingDailyInvDA.GetBillingDailyInvByID(dataBase, tran, billingCredenceInfo.DocID, billingCredenceInfo.BillingObjectID, "Y", whLoginID);
                                if ((billingDailyInvInfo != null) && (billingDailyInvInfo.IsStatement != "Y"))
                                {
                                    billingDailyInvInfo.OwnerID = billingCredenceInfo.BillingObjectID;
                                    billingDailyInvInfo.Days = billingCredenceInfo.DocID;
                                    billingDailyInvInfo.IsStatement = "Y";
                                    billingDailyInvInfo.IsStorageCharges = "Y";
                                    billingDailyInvInfo.UpdatedBy = this.pubParasInfo.UserID;
                                    billingDailyInvDA.UpdateBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                                }
                            }
                            else if (billingCredenceInfo.CredenceType == "InvCharges")
                            {
                                billingDailyInvInfo = billingDailyInvDA.GetBillingDailyInvByID(dataBase, tran, billingCredenceInfo.DocID, billingCredenceInfo.BillingObjectID, "N", whLoginID);
                                if ((billingDailyInvInfo != null) && (billingDailyInvInfo.IsStatement != "Y"))
                                {
                                    billingDailyInvInfo.OwnerID = billingCredenceInfo.BillingObjectID;
                                    billingDailyInvInfo.Days = billingCredenceInfo.DocID;
                                    billingDailyInvInfo.IsStatement = "Y";
                                    billingDailyInvInfo.IsStorageCharges = "N";
                                    billingDailyInvInfo.UpdatedBy = this.pubParasInfo.UserID;
                                    billingDailyInvDA.UpdateBillingDailyInv(dataBase, tran, billingDailyInvInfo, this.dataBaseID);
                                }
                            }
                        }
                    }
                    StatementID = obj.StatementID;
                    new AuditDataBLL().InsertAuditData<StatementInfo>(dataBase, tran, oldstatementInfoInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "STATEMENT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return StatementID;
        }

        public string UpdateStatementStatus(StatementInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            string StatementID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (!statementDA.CheckStatementIDUnique(dataBase, tran, obj.StatementID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9200_001", this.pubParasInfo.UserLanguage, new List<string> { obj.StatementID });
                }
                StatementInfo oldstatementInfoInfo = statementDA.GetStatementByID(dataBase, tran, obj.StatementID, this.dataBaseID[0]);
                obj.UpdatedBy = this.pubParasInfo.UserID;
                obj.UpdatedDate = DateTime.Now;
                if (statementDA.UpdateStatement(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                {
                    StatementID = obj.StatementID;
                    new AuditDataBLL().InsertAuditData<StatementInfo>(dataBase, tran, oldstatementInfoInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "STATEMENT", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return StatementID;
        }
    }
}

